﻿using System.Collections.Generic;
using Foothill.Common;
using Foothill.DataAccess.Repository;
using Foothill.DataAccess.Interface;
using Foothill.DataAccess;

namespace Foothill.Service.MiscInfo
{
    using Exceptions;

    public class MiscInfoService : IMiscInfoService
    {
        private IMiscInfoRepo _miscInfoRepo;

        public MiscInfoService(IMiscInfoRepo miscInfoRepo)
        {
            _miscInfoRepo = miscInfoRepo;
        }
        
        public IDictionary<int, string> GetCities(int provinceId)
        {
            try
            {
                return _miscInfoRepo.GetCities(provinceId);
            }
            catch (DataAccessException exception)
            {
                throw new ServiceException("Data Access Error", exception);
            }
        }

        public IDictionary<int, string> GetMinPriceDict()
        {
            IDictionary<int, string> result = new Dictionary<int, string>();

            result.Add(0, "0");
            result.Add(500, "500");
            result.Add(1000, "1000");
            result.Add(2000, "2000");
            result.Add(3000, "3000");
            result.Add(5000, "5000");
            result.Add(8000, "8000");
            result.Add(10000, "10000");
            result.Add(15000, "15000");
            result.Add(20000, "20000");
            result.Add(25000, "25000");
            result.Add(30000, "30000");
            result.Add(40000, "40000");
            result.Add(50000, "50000");
            result.Add(60000, "60000");
            result.Add(70000, "70000");
            result.Add(80000, "80000");
            result.Add(90000, "90000");
            result.Add(100000, "100000");

            return result;
        }

        public IDictionary<int, string> GetMaxPriceDict()
        {
            IDictionary<int, string> result = new Dictionary<int, string>();

            result.Add(1000, "1000");
            result.Add(2000, "2000");
            result.Add(3000, "3000");
            result.Add(5000, "5000");
            result.Add(8000, "8000");
            result.Add(10000, "10000");
            result.Add(15000, "15000");
            result.Add(20000, "20000");
            result.Add(25000, "25000");
            result.Add(30000, "30000");
            result.Add(40000, "40000");
            result.Add(50000, "50000");
            result.Add(60000, "60000");
            result.Add(70000, "70000");
            result.Add(80000, "80000");
            result.Add(90000, "90000");
            result.Add(100000, "100000");
            result.Add(GlobalValues.DropdownMaxValue, "100000+");

            return result;
        }

        public IDictionary<int, string> GetMinKilometersDict()
        {
            IDictionary<int, string> result = new Dictionary<int, string>();

            result.Add(0, "0");
            result.Add(5000, "5k");
            result.Add(10000, "10k");
            result.Add(15000, "15k");
            result.Add(20000, "20k");
            result.Add(30000, "30k");
            result.Add(40000, "40k");
            result.Add(50000, "50k");
            result.Add(60000, "60k");
            result.Add(70000, "70k");
            result.Add(80000, "80k");
            result.Add(90000, "90k");
            result.Add(100000, "100k");
            result.Add(150000, "150k");
            return result;
        }

        public IDictionary<int, string> GetMaxKilometersDict()
        {
            IDictionary<int, string> result = new Dictionary<int, string>();

            result.Add(5000, "5k");
            result.Add(10000, "10k");
            result.Add(15000, "15k");
            result.Add(20000, "20k");
            result.Add(30000, "30k");
            result.Add(40000, "40k");
            result.Add(50000, "50k");
            result.Add(60000, "60k");
            result.Add(70000, "70k");
            result.Add(80000, "80k");
            result.Add(90000, "90k");
            result.Add(100000, "100k");
            result.Add(150000, "150k");
            result.Add(GlobalValues.DropdownMaxValue, "> 150k");
            return result;
        }

        public IDictionary<int, string> GetPriceRangeDict()
        {
            Dictionary<int, string> result = new Dictionary<int, string>();

            foreach (var p in GlobalValues.PriceRangeDict)
            {
                result.Add((int)p.Key, p.Value);
            }

            return result;
        }

        public IDictionary<int, string> GetMileageRangeDict()
        {
            IDictionary<int, string> result = new Dictionary<int, string>();

            foreach (var p in GlobalValues.MileageRangeDict)
            {
                result.Add((int)p.Key, p.Value);
            }

            return result;
        }

        public int GetMaxMileageNumber(MileageRange range)
        {
            // ZeroTo5K, FiveKTo10K, TenKTo20K, TwentyKTo30K, ThirtyKTo40K, FourtyKTo50K, MoreThan50K, Any
            int result = 0;
            switch (range)
            {
                case MileageRange.Any:
                    result = GlobalValues.DropdownMaxValue;
                    break;
                case MileageRange.LessThan100K:
                    result = 100000;
                    break;
                case MileageRange.LessThan10K:
                    result = 10000;
                    break;
                case MileageRange.LessThan20K:
                    result = 20000;
                    break;
                case MileageRange.LessThan30K:
                    result = 30000;
                    break;
                case MileageRange.LessThan50K:
                    result = 50000;
                    break;
                case MileageRange.LessThan5K:
                    result = 5000;
                    break;
                case MileageRange.LessThan80K:
                    result = 80000;
                    break;
            }

            return result;
        }

        public void ConvertPriceEnumsToRange(PriceRange priceRangeFrom, PriceRange priceRangeTo, out int from, out int to)
        {
            from = 0;
            to = 0;

            if (priceRangeFrom == PriceRange.Any)
            {
                if (priceRangeTo == PriceRange.Any)
                {
                    return;
                }
                else
                {
                    to = ConvertPriceEnumToNumber(priceRangeTo);
                    return;
                }
            }
            else
            {
                from = ConvertPriceEnumToNumber(priceRangeFrom);
                if (priceRangeTo == PriceRange.Any)
                {
                    to = GlobalValues.DropdownMaxValue;
                }
                else
                {
                    to = ConvertPriceEnumToNumber(priceRangeTo);
                    if (from > to)
                    {
                        int tmp = from;
                        from = to;
                        to = tmp;
                    }
                }
            }
        }

        internal int ConvertPriceEnumToNumber(PriceRange priceRange)
        {
            int price = 0;
            switch (priceRange)
            {
                case PriceRange.Any:
                    break;
                case PriceRange.P5K:
                    price = 5000;
                    break;
                case PriceRange.P10K:
                    price = 10000;
                    break;
                case PriceRange.P20K:
                    price = 20000;
                    price = 0;
                    break;
                case PriceRange.P30K:
                    price = 30000;
                    break;
                case PriceRange.P50K:
                    price = 50000;
                    break;
                case PriceRange.P80K:
                    price = 80000;
                    break;
                case PriceRange.P100K:
                    price = 100000;
                    break;
                case PriceRange.P150K:
                    price = 150000;
                    break;
                case PriceRange.P200K:
                    price = 200000;
                    break;
            }

            return price;
        }
    }
}
